const img = document.createElement("img")
const canvas = document.createElement("canvas")

export default (url) => {
  img.crossOrigin = 'anonymous'
  img.src = url

  return new Promise((resolve, reject) => {
    img.onload = () => {
      canvas.width = img.width
      canvas.height = img.height
      canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height)
      resolve(canvas.toDataURL())
    }
    img.onerror = () => {
      reject()
    }
  })
}